import 'package:app_core/app_core.dart';
import 'package:app_core/src/utils/screen_adapter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_svg/svg.dart';

// 功能全面且简洁的 AppBar 封装
///
/// 使用示例：
/// ```dart
/// // 最简洁 - 自动适配主题
/// MyAppBar('标题')
///
/// // 简单自定义
/// MyAppBar('标题', rightWidget: Icon(Icons.search))
///
/// // 完全自定义
/// MyAppBar(
///   '标题',
///   backgroundColor: Colors.blue,
///   titleColor: Colors.white,
///   showBack: false,
///   onBackPressed: customBack,
/// )
class MyAppBar extends StatelessWidget implements PreferredSizeWidget {
  final String? title;

  final bool showBack;
  final VoidCallback? onBackPressed;

  final Color? backgroundColor;
  final Color? titleColor;
  final TextStyle? titleStyle;
  final double elevation;
  final double scrolledUnderElevation;
  final double titleSpacing;

  final Widget? leftWidget;
  final Widget? centerWidget;
  // final Widget? rightWidget;
  final List<Widget>? actions;
  final PreferredSizeWidget? bottom;
  final SystemUiOverlayStyle? statusBarStyle;

  /// backgroundColor: Colors.transparent,  //设置为透明才有效果
  final Widget Function(Widget)? backgroundBuilder;

  const MyAppBar(
    this.title, {
    super.key,
    this.actions,
    this.showBack = true,
    this.onBackPressed,
    this.backgroundColor,
    this.titleColor,
    this.titleStyle,
    this.elevation = 0,
    this.scrolledUnderElevation = 0,
    this.titleSpacing = 6,
    this.leftWidget,
    this.centerWidget,
    this.bottom,
    this.statusBarStyle,
    this.backgroundBuilder,
  });

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final colorScheme = theme.colorScheme;

    Widget appBar = AppBar(
      backgroundColor: backgroundColor ?? appGlobalConfig.widgetConfig.navBackgroundColor,
      elevation: elevation,
      titleSpacing: titleSpacing,
      leading: _buildLeading(context, colorScheme),

      title: _buildTitle(context, colorScheme),

      actions: actions != null ? [...actions!, SizedBox(width: 8.0)] : null,

      bottom: bottom,

      /// 不自动处理状态栏，保持原有行为
      systemOverlayStyle: statusBarStyle,

      /// 自动处理返回按钮
      automaticallyImplyLeading: false,

      /// 禁用滑动时的渐变效果
      scrolledUnderElevation: scrolledUnderElevation,
    );

    if (backgroundBuilder != null) {
      appBar = backgroundBuilder!(appBar);
    }
    return appBar;
  }

  /// 构建左侧按钮
  Widget? _buildLeading(BuildContext context, ColorScheme colorScheme) {
    if (!showBack) return null;
    if (leftWidget != null) {
      return leftWidget!;
    }

    /// 使用原有的返回按钮配置
    return IconButton(icon: appGlobalConfig.widgetConfig.backIcon, onPressed: onBackPressed ?? () => Navigator.pop(context));
  }

  /// 构建标题
  Widget? _buildTitle(BuildContext context, ColorScheme colorScheme) {
    if (centerWidget != null) {
      return centerWidget!;
    }

    if (title == null || title!.isEmpty) {
      return null;
    }

    return Text(
      title!,
      style: titleStyle ?? appGlobalConfig.widgetConfig.navTitleStyle.copyWith(color: titleColor),
      overflow: TextOverflow.ellipsis,
      maxLines: 1,
    );
  }

  @override
  Size get preferredSize => const Size.fromHeight(kToolbarHeight);

  /// 创建 SVG 图标按钮
  static Widget svgIcon({required String icon, VoidCallback? onTap, double? size, Color? color, EdgeInsets? padding}) {
    return AppGesture(
      onTap: onTap,
      child: Padding(
        padding: padding ?? EdgeInsets.all(0),
        child: SvgPicture.asset(
          icon,
          width: size ?? 32.rs,
          height: size ?? 32.rs,
          colorFilter: color != null ? ColorFilter.mode(color, BlendMode.srcIn) : null,
        ),
      ),
    );
  }

  /// 创建 Image 图标按钮
  static Widget imageIcon({required String icon, VoidCallback? onTap, double? size, Color? color, EdgeInsets? padding, BoxFit? fit}) {
    return AppGesture(
      onTap: onTap,
      child: Padding(
        padding: padding ?? EdgeInsets.all(0),
        child: Image.asset(icon, width: size ?? 32.rs, height: size ?? 32.rs, color: color, fit: fit ?? BoxFit.contain),
      ),
    );
  }
}
